package com.xoa.service.workflow.flowSort.impl;

import com.xoa.dao.workflow.FlowSortMapper;
import com.xoa.dao.workflow.FlowTypeModelMapper;
import com.xoa.model.workflow.FlowSort;
import com.xoa.model.workflow.FlowTypeModel;
import com.xoa.service.workflow.flowSort.FlowSortService;
import com.xoa.util.ToJson;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by gsb on 2017/7/18.
 */
@Service
public class FlowSortServiceImpl implements FlowSortService {

    @Resource
    private FlowSortMapper flowSortMapper;
    @Resource
    private FlowTypeModelMapper flowTypeModelMapper;

    /**
     * 创建作者:   张航宁
     * 创建日期:   2017/7/18
     * 方法介绍:   获取一级菜单下的所有流程信息
     * @return
     */
    @Override
    public ToJson<FlowSort> getFlowTypes() {
        ToJson<FlowSort> json = new ToJson<FlowSort>();
        try{
            List<FlowSort> flowSorts = new ArrayList<FlowSort>();
            getFlowSorts(flowSorts,null,null,0);
            json.setObj(flowSorts);
            json.setMsg("ok");
            json.setFlag(0);
        }catch (Exception e){
            e.printStackTrace();
            json.setMsg("err");
            json.setFlag(1);
        }

        return json;
    }

    /**
     * 创建作者:   张航宁
     * 创建日期:   2017/7/18
     * 方法介绍:   递归获取一级菜单下的所有流程信息
     * @return
     */
    private void getFlowSorts(List<FlowSort> flowSorts ,List<FlowSort> flowSortsChildren,FlowSort parentF, Integer sortId) {
        // 查询一级菜单
        if(sortId==0){
            flowSorts.addAll(flowSortMapper.getChildDatas(0));
        }
        // 如果parentF 为查询一级菜单和信息
        if(parentF==null){
            // 根据一级菜单的id查询流程信息
            if(flowSorts!=null&&flowSorts.size()>0){
                for (FlowSort f:flowSorts) {
                    // 判断一级菜单是否为空
                    if(f!=null&&f.getSortId()!=null&&f.getSortId()!=0){
                        // 获取一级菜单流程信息
                        List<FlowTypeModel> flowTypeModels = flowTypeModelMapper.selectBySortid(f.getSortId());
                        List<FlowTypeModel> AllFlow=new ArrayList<FlowTypeModel>();
                        for(FlowTypeModel ff:flowTypeModels){
                            AllFlow.add(ff);
                        }
                        List<FlowSort> childDatas = flowSortMapper.getChildDatas(f.getSortId());
                        if(childDatas!=null&&childDatas.size()>0){
                            for (FlowSort f2:childDatas) {
                                List<FlowTypeModel> flowTypeModels2 = flowTypeModelMapper.selectBySortid(f2.getSortId());
                                for(FlowTypeModel ff:flowTypeModels2) {
                                    AllFlow.add(ff);
                                }
                            }
                        }
                     /*   // 查询二级菜单
                        List<FlowSort> flowSortsChildrens = flowSortMapper.getChildDatas(f.getSortId());
                        if(flowSortsChildrens!=null&&flowSortsChildrens.size()>0){
                            for (FlowSort f2:flowSortsChildrens) {
                                List<FlowTypeModel> flowTypeModels2 = flowTypeModelMapper.selectBySortid(f2.getSortId());
                              f2.setFlowTypeModels(flowTypeModels2);
                            }
                        }*/
                        f.setFlowTypeModels(AllFlow);
                    }
                }
            }
        }else{
            for (FlowSort f:flowSortsChildren) {
                // 判断一级菜单是否为空
                if(f!=null&&f.getSortId()!=null&&f.getSortId()!=0){
                    // 获取一级菜单流程信息
                    List<FlowTypeModel> flowTypeModels = flowTypeModelMapper.selectBySortid(f.getSortId());
                    List<FlowTypeModel> AllFlow=new ArrayList<FlowTypeModel>();
                    for(FlowTypeModel ff:flowTypeModels){
                        AllFlow.add(ff);
                    }
                    if(f.getHaveChild().equals(1)){
                    List<FlowSort> childDatas = flowSortMapper.getChildDatas(f.getSortId());
                    if(childDatas!=null&&childDatas.size()>0){
                        for (FlowSort f2:childDatas) {
                            List<FlowTypeModel> flowTypeModels2 = flowTypeModelMapper.selectBySortid(f2.getSortId());
                            for(FlowTypeModel ff:flowTypeModels2) {
                                AllFlow.add(ff);
                            }
                        }
                    }
                    }
                /*    // 查询二级菜单
                    if(f.getHaveChild().equals(1)){
                        List<FlowSort> flowSortsChildrens = flowSortMapper.getChildDatas(f.getSortId());
                        if(flowSortsChildrens!=null&&flowSortsChildrens.size()>0){
                            for (FlowSort f2:flowSortsChildrens) {
                                List<FlowTypeModel> flowTypeModels2 = flowTypeModelMapper.selectBySortid(f2.getSortId());
                                f2.setFlowTypeModels(flowTypeModels2);
                            }
                        }
                    }*/
                    f.setFlowTypeModels(AllFlow);
                }

            }
        }

    }

    /**
     * 牛江丽
     * 按层级获取流程分类
     * @return
     */
    public ToJson<FlowSort> getSortAndType(){
        ToJson<FlowSort> json=new ToJson<>(1,"error");
        try {
            List<FlowSort> list=flowSortMapper.getChildDatas(0);
            getSortAndTypeByDigui(list);
            json.setObj(list);
            json.setFlag(0);
            json.setMsg("ok");
        }catch (Exception e){
            e.printStackTrace();
        }
        return json;
    }

    @Override
    public ToJson<FlowSort> selectType(HttpServletRequest request, Integer flowId) {
        ToJson<FlowSort> toJson = new ToJson<FlowSort>();
        String datas = flowSortMapper.selectType(flowId);
        if ("1".equals(datas)){
            toJson.setMsg("公文");
            toJson.setFlag(0);
            toJson.setCode("0");
        }else {
            toJson.setMsg("不是公文");
            toJson.setFlag(1);
            toJson.setCode("1");
        }
        return toJson;
    }

    void getSortAndTypeByDigui(List<FlowSort> list){
        for(FlowSort flowSort:list){
            List<FlowSort> list1=flowSortMapper.getChildDatas(flowSort.getSortId());
            flowSort.setFlowTypeModels(flowTypeModelMapper.selByFlowSort(flowSort.getSortId()));
            if(list1.size()>0){
                flowSort.setChilds(list1);
                flowSort.setHaveChild("1");
                getSortAndTypeByDigui(list1);
            }else{
                continue;
            }
        }
    }

    /**
     * 获取子流程菜单
     * @return
     */
    @Override
    public ToJson<FlowSort> getSonFlowTypes() {
        ToJson<FlowSort> json = new ToJson<FlowSort>();
        try{
            List<FlowSort> flowSorts = new ArrayList<FlowSort>();
            getSonFlowSorts(flowSorts,null,null,0);
            json.setObj(flowSorts);
            json.setMsg("ok");
            json.setFlag(0);
        }catch (Exception e){
            e.printStackTrace();
            json.setMsg("err");
            json.setFlag(1);
        }

        return json;
    }
    private void getSonFlowSorts(List<FlowSort> flowSorts ,List<FlowSort> flowSortsChildren,FlowSort parentF, Integer sortId) {
        // 查询一级菜单
        if(sortId==0){
            flowSorts.addAll(flowSortMapper.getSonChildDatas(0));
        }
        // 如果parentF 为查询一级菜单和信息
        if(parentF==null){
            // 根据一级菜单的id查询流程信息
            if(flowSorts!=null&&flowSorts.size()>0){
                for (FlowSort f:flowSorts) {
                    // 判断一级菜单是否为空
                    if(f!=null&&f.getSortId()!=null&&f.getSortId()!=0){
                        // 获取一级菜单流程信息
                        List<FlowTypeModel> flowTypeModels = flowTypeModelMapper.selectBySortid(f.getSortId());
                        List<FlowTypeModel> AllFlow=new ArrayList<FlowTypeModel>();
                        for(FlowTypeModel ff:flowTypeModels){
                            AllFlow.add(ff);
                        }
                        List<FlowSort> childDatas = flowSortMapper.getSonChildDatas(f.getSortId());
                        if(childDatas!=null&&childDatas.size()>0){
                            for (FlowSort f2:childDatas) {
                                List<FlowTypeModel> flowTypeModels2 = flowTypeModelMapper.selectBySortid(f2.getSortId());
                                for(FlowTypeModel ff:flowTypeModels2) {
                                    AllFlow.add(ff);
                                }
                            }
                        }
                     /*   // 查询二级菜单
                        List<FlowSort> flowSortsChildrens = flowSortMapper.getChildDatas(f.getSortId());
                        if(flowSortsChildrens!=null&&flowSortsChildrens.size()>0){
                            for (FlowSort f2:flowSortsChildrens) {
                                List<FlowTypeModel> flowTypeModels2 = flowTypeModelMapper.selectBySortid(f2.getSortId());
                              f2.setFlowTypeModels(flowTypeModels2);
                            }
                        }*/
                        f.setFlowTypeModels(AllFlow);
                    }
                }
            }
        }else{
            for (FlowSort f:flowSortsChildren) {
                // 判断一级菜单是否为空
                if(f!=null&&f.getSortId()!=null&&f.getSortId()!=0){
                    // 获取一级菜单流程信息
                    List<FlowTypeModel> flowTypeModels = flowTypeModelMapper.selectBySortid(f.getSortId());
                    List<FlowTypeModel> AllFlow=new ArrayList<FlowTypeModel>();
                    for(FlowTypeModel ff:flowTypeModels){
                        AllFlow.add(ff);
                    }
                    if(f.getHaveChild().equals(1)){
                        List<FlowSort> childDatas = flowSortMapper.getChildDatas(f.getSortId());
                        if(childDatas!=null&&childDatas.size()>0){
                            for (FlowSort f2:childDatas) {
                                List<FlowTypeModel> flowTypeModels2 = flowTypeModelMapper.selectBySortid(f2.getSortId());
                                for(FlowTypeModel ff:flowTypeModels2) {
                                    AllFlow.add(ff);
                                }
                            }
                        }
                    }
                /*    // 查询二级菜单
                    if(f.getHaveChild().equals(1)){
                        List<FlowSort> flowSortsChildrens = flowSortMapper.getChildDatas(f.getSortId());
                        if(flowSortsChildrens!=null&&flowSortsChildrens.size()>0){
                            for (FlowSort f2:flowSortsChildrens) {
                                List<FlowTypeModel> flowTypeModels2 = flowTypeModelMapper.selectBySortid(f2.getSortId());
                                f2.setFlowTypeModels(flowTypeModels2);
                            }
                        }
                    }*/
                    f.setFlowTypeModels(AllFlow);
                }

            }
        }

    }

}
